

from django.shortcuts import render
from django.db import connection
import requests
import json
import json
import time
import traceback
import pymysql
from pymysql.converters import escape_string
import re
import emoji
import requests
import time
import os

database_name = "study_note"
path = r"C:\Users\yxb\Desktop\testttt\\"


class File_object:
    def __init__(self, score, file_name, title, content):
        self.score = score
        self.file_name = file_name
        self.title = title
        self.content = content


# 判断是否全是英文? (不能有空格)
def bool_all_english(string):
    pattern = re.compile(r'[^a-zA-Z]+')
    result = pattern.findall(string)
    if len(result) > 0:
        return False
    return True


# 去掉 所有的英文
def remove_all_english(string):
    new_str = re.sub("[a-zA-Z]+", " ", string)  # 去掉所有的英文
    return new_str


# chinese_words_match("安装教程" , "Java在虚拟机下面的安装示范 豆腐干豆腐干士大夫敢死队风格")
# chinese_words_match("菜市场买豆腐", "Java在虚拟机下面的安装示范 豆腐干豆腐干士大夫敢死队风格")
def chinese_words_match(keywords, text):
    keywords = remove_all_english(keywords)
    keywords = keywords.replace(" ", "")
    score = 0
    word_list = list(keywords)
    for word in word_list:
        if word != '':
            if word in text:
                # print("cccccc")
                score = score + 5  # 配对成功 权重 + 5
    return score


def search_result_rank(keywords, title_, content_):
    score = 0
    keyword_list = keywords.split(" ")
    for word in keyword_list:
        if_all_eng = bool_all_english(word)
        if if_all_eng:
            if len(word) > 0 and word.lower() in title_.lower():
                # print("aaaaaa")
                score = score + 20
            if len(word) > 0 and word.lower() in content_.lower():
                # print("bbbbb")
                score = score + 8
        if not if_all_eng:
            score = score + chinese_words_match(keywords, title_) * 6  # title 六倍权重
            score = score + chinese_words_match(keywords, content_) * 3  # content 三倍权重
    return score


print(search_result_rank(" ibm 服务 ", "Java 服务器 部署 docker ", "阿三大苏打实打实的菜市场买apple"))


def get_file_list(folder_path):
    file_list = []
    dirs = os.listdir(folder_path)
    for ii in dirs:
        # print(ii)  # 文件名/目录名
        full_path = os.path.join(path, ii)
        print(full_path)  # 全路径
        if os.path.isfile(full_path):
            file_list.append(full_path)
    return file_list


def get_file_data(file_path):
    with open(file_path, "r", encoding="utf-8") as f:
        file_content = f.read()
        json_data = json.loads(file_content)
        return json_data


def get_timeStamp():
    return str(round(time.time() * 1000))





def start(keywords):
    listttt = get_file_list(path)
    temp_list = []
    for file_path in listttt:
        score = 0
        note_obj = get_file_data(file_path)
        title = note_obj["title"]
        content = note_obj["content"]
        score = search_result_rank(keywords, title, content)
        print(score)
        print(file_path)
        print(title)
        # print(content)
        print("==================================")
        temp_list.append(File_object(score, file_path, title, content))

    return_list=[]
    temp_list = sorted(temp_list, key=lambda x: x.score, reverse=True)
    for obj in temp_list:
        print(obj.score)
        return_list.append(obj.__dict__)
    json_str = json.dumps(return_list, ensure_ascii=False)
    # print(json_str)
    return json_str


class User:
    def __init__(self, age, name):
        self.age=age
        self.name=name

def index(request, search_text):
    print(search_text)
    print(search_text)
    resp = {
        'title':'    search >>>  ',
        "key": "api_request.content" 
    }
    jsonnn=start("java docker")
    myList = []
    obj_0 = User(12,"啊实")
    obj_1 = User(21,"哈哈哈哈\n\n\n哈哈哈\n\n红红火火恍                 恍惚惚")
    myList.append(obj_0.__dict__)
    myList.append(obj_1.__dict__)
    return render(request,'search/inde.html', {'data': json.dumps(start("java docker"), ensure_ascii=True)})
    # return render(request,'search/inde.html', {'data': json.dumps(myList , ensure_ascii=True)})


'''


'''


